package com.example.mydy;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.mydy.adapter.UserAnnouncementAdapter;
import com.example.mydy.model.Announcement;
import com.example.mydy.utils.MockDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 公告列表页面 - 专为普通用户设计，显示所有公告并可查看详情
 */
public class AnnouncementListActivity extends AppCompatActivity implements UserAnnouncementAdapter.OnAnnouncementClickListener {
    private static final String TAG = "AnnouncementListActivity";
    
    private RecyclerView rvAnnouncements;
    private SwipeRefreshLayout swipeRefreshLayout;
    private TextView tvNoAnnouncements;
    private Button btnBack;
    
    private List<Announcement> announcementList = new ArrayList<>();
    private UserAnnouncementAdapter announcementAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_announcement_list);
        
        initViews();
        setupRecyclerView();
        setupListeners();
        
        // 加载数据
        loadAnnouncements();
    }
    
    private void initViews() {
        rvAnnouncements = findViewById(R.id.rv_announcements);
        swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
        tvNoAnnouncements = findViewById(R.id.tv_no_announcements);
        btnBack = findViewById(R.id.btn_back);
    }
    
    private void setupRecyclerView() {
        announcementAdapter = new UserAnnouncementAdapter(this, announcementList, this);
        rvAnnouncements.setLayoutManager(new LinearLayoutManager(this));
        rvAnnouncements.setAdapter(announcementAdapter);
    }
    
    private void setupListeners() {
        swipeRefreshLayout.setOnRefreshListener(this::loadAnnouncements);
        
        btnBack.setOnClickListener(v -> finish());
    }
    
    private void loadAnnouncements() {
        swipeRefreshLayout.setRefreshing(true);
        
        // 获取所有已发布的公告
        List<Announcement> announcements = MockDataManager.getInstance().getAllAnnouncements();
        
        // 更新UI
        announcementList.clear();
        announcementList.addAll(announcements);
        announcementAdapter.notifyDataSetChanged();
        
        swipeRefreshLayout.setRefreshing(false);
        
        // 显示或隐藏"暂无公告"提示
        if (announcementList.isEmpty()) {
            tvNoAnnouncements.setVisibility(View.VISIBLE);
        } else {
            tvNoAnnouncements.setVisibility(View.GONE);
        }
    }

    @Override
    public void onAnnouncementClick(Announcement announcement) {
        // 点击公告时，跳转到公告详情页面
        Intent intent = new Intent(this, AnnouncementDetailActivity.class);
        intent.putExtra("announcement_id", announcement.getId());
        startActivity(intent);
    }
} 